﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace TwitterBobby
{
    public class TwitterBobbyService : ITwitterBobbyService
    {
        public IEnumerable<ChannelDTO> GetAllChannels()
        {
            TwitterBobbyEntities1 context = new TwitterBobbyEntities1();
            var channelsList = context.Channels.OrderBy(x => x.ChannelID).ToList();
            IEnumerable<ChannelDTO> result = channelsList.Select(c => ChannelDTO.CreateFromChannel(c));
            return result;
        }
        
        public IEnumerable<MessageDTO> GetMessagesBYChannelName(string channelName)
        {
            TwitterBobbyEntities1 context = new TwitterBobbyEntities1();
            var messages = (
                from m in context.Messages
                join c in context.Channels 
                on m.ChannelID equals c.ChannelID
                where (c.ChannelName==channelName)
                orderby m.MessageDate descending
                select m).Take(20).ToList();
            IEnumerable<MessageDTO> result = messages.Select(c => MessageDTO.CreateFromMessage(c));
            return result;
        }

        public void CreateChannel(string channelName)
        {
            TwitterBobbyEntities1 context = new TwitterBobbyEntities1();
            var channel = from c in context.Channels
                          where c.ChannelName == channelName
                          select c;
            bool isThereSuchAChannel = channel.Any();

            if (isThereSuchAChannel)
            {
                var channelID = (from c in context.Channels
                                 where c.ChannelName == channelName
                                 select c.ChannelID).FirstOrDefault();
                IEnumerable<Message> toDelete = (from m in context.Messages
                                                 where m.ChannelID == channelID
                                                 select m).ToList();
                foreach (var item in toDelete)
                {
                    context.DeleteObject(item);
                }
            }
            else
            {
                Channel channelToAdd = new Channel();
                channelToAdd.ChannelName = channelName;
                context.Channels.AddObject(channelToAdd);
            }
            context.SaveChanges();
        }

        public void PostMessage(MessageToPOST msg)
        {
            TwitterBobbyEntities1 context = new TwitterBobbyEntities1();
            Message result = new Message();
            result.ChannelID = (from c in context.Channels
                                where (c.ChannelName == msg.MsgChannel)
                                select c.ChannelID).SingleOrDefault();
            result.MessageDate = msg.MsgDate;
            result.Text = msg.MsgText;
            context.Messages.AddObject(result);
            context.SaveChanges();
        }
    }
}
